package Controller;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.SwingUtilities;

import view.Home;
import Multithreads.MyCallable;

public class Controller extends AbstractAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Home home;
	private String callPutFlag;
	private double s;
	private double x;
	private double t;
	private double r;
	private double b;
	private double v;
	private int nSteps;
	private int nSimulations;
	private int poolSize;
	private int nbAlgo;
	private double res;


	public Controller(Home h){
		super("GO !");
		this.home = h;
	}
	
	
	public void actionPerformed(ActionEvent e){
		SwingUtilities.invokeLater(new Runnable(){
			private MyCallable myCallable;

			public void run(){	

				poolSize = home.getFieldPoolSize();
				nbAlgo = home.getFieldNbAlgo();
				callPutFlag = home.getFieldCallPutFlag();
				s = home.getFieldS();
				x = home.getFieldX();
				t = home.getFieldT();
				r = home.getFieldR();
				b = home.getFieldB();
				v = home.getFieldV();
				nSteps = home.getFieldNSteps();
				nSimulations = home.getFieldNSimulations();
				

				//Recuperation du temps de depart;
				long timeStart = System.currentTimeMillis();
				
				myCallable = new MyCallable(callPutFlag, s, x, t, r, b, v, nSteps, nSimulations, poolSize, nbAlgo);
				
				//Recuperation du temps d'arrivee
				long timeEnd = System.currentTimeMillis();
				
				//Affichage temps de depart et temps d'arrivee
				float  time = timeEnd- timeStart;
				System.out.println("Total time: " + time/1000 + "s");
				
				try {
					res = myCallable.getRes();
					home.writeResField(res);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}


	public Home getHome() {
		return home;
	}


	public void setHome(Home home) {
		this.home = home;
	}

}
